class Solution:
    def minPathSum(self , matrix ):
        rows = len(matrix)
        cols = len(matrix[0])
        dp = [[0]*cols for _ in range(rows)]
        for col in range(cols):
            dp[0][col] = sum(matrix[0][:col+1])
        for row in range(rows):
            sum_ = 0
            for i in range(row+1):
                sum_ += matrix[i][0]
            dp[row][0] = sum_
        for row in range(1,rows):
            for col in range(1,cols):
                dp[row][col] = min(dp[row-1][col],dp[row][col-1])+matrix[row][col]
        return dp[-1][-1]